from django.contrib import admin
from .models import AIConfig, ChatSession


@admin.register(AIConfig)
class AIConfigAdmin(admin.ModelAdmin):
    list_display = ('name', 'model_name', 'is_active', 'created_at')
    list_filter = ('is_active', 'created_at')
    search_fields = ('name', 'model_name')
    readonly_fields = ('created_at', 'updated_at')
    
    fieldsets = (
        ('基本配置', {
            'fields': ('name', 'is_active')
        }),
        ('API配置', {
            'fields': ('api_key', 'api_url', 'model_name')
        }),
        ('模型参数', {
            'fields': ('max_tokens', 'temperature'),
            'description': 'max_tokens: 最大生成令牌数, temperature: 控制回复的随机性(0-1)'
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at'),
            'classes': ('collapse',)
        }),
    )


@admin.register(ChatSession)
class ChatSessionAdmin(admin.ModelAdmin):
    list_display = ('session_id', 'context_length', 'created_at', 'updated_at')
    list_filter = ('created_at', 'updated_at')
    search_fields = ('session_id',)
    readonly_fields = ('created_at', 'updated_at')
    
    def context_length(self, obj):
        return len(obj.context) if obj.context else 0
    context_length.short_description = '上下文长度'
